//
//  LKComposeView.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 2016/10/8.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit
import pop
class LKComposeView: UIView {
    //懒加载数组
    lazy var btnArray = [LKComposeBtn]()
    var targetVC : UIViewController?
    func show(target:UIViewController?) {
        self.targetVC = target
        target?.view.addSubview(self)
    }
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setMyUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setMyUI () {
        //截取当前屏幕作为背景图
        let bgImageView = UIImageView(image: UIImage.screenShot().applyLightEffect())
        //添加子视图
        self.addSubview(bgImageView)
        //添加背景文字图片
        let slogan = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
        self.addSubview(slogan)
        slogan.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(100)
        }
        addChildButtons()
    }
    //添加按钮
    private func addChildButtons () {
        //计算间距
        let margin = (ScreenWidth - 3 * composeBtnW) / (3 + 1)
        //添加按钮
        for i in 0..<6
        {
            let btn = LKComposeBtn()
            btn.setTitle("位置", for: .normal)
            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_lbs"), for: .normal)
            //计算行和列
            let row = i / 3
            let col = i % 3
            //计算x,y坐标
            let btnX = margin + (composeBtnW + margin ) * CGFloat( col)
            let btnY = (composeBtnH + margin ) * CGFloat(row) + ScreenHeight
            btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnW, height: composeBtnH)
            //给按钮添加点击事件
            btn.addTarget(self, action: #selector(didClickComposeBtn(btn:)), for: .touchUpInside)
            self.addSubview(btn)
            //把按钮添加到数组之中
            btnArray.append(btn)
        }
    }
    
    //按钮出现的动画
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        //遍历数组
        for btn in btnArray.enumerated()
        {
            btnAnimation(btn: btn.element, index: btn.offset, isShowing: false)
        }
    }
    //按钮消失的动画
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for btn in btnArray.reversed().enumerated() {
            btnAnimation(btn: btn.element, index: btn.offset, isShowing: true)
        }
        //延迟执行移除按钮
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) { 
            self.removeFromSuperview()
        }
    }
    
    //动画方法封装
    private func btnAnimation (btn: LKComposeBtn, index: Int, isShowing: Bool) {
        //实例化pop弹簧的动画对象
        let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置动画的基本属性
        animation?.springSpeed  = 12
        animation?.springBounciness = 10
        //设置动画时间
        animation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        //设置toValue结构体,因为直接设置程序会崩溃,所以要包装成nsvalue
        animation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btn.center.y+(isShowing == true ? 350 : -350)))
        //将动画 添加到按钮上
        btn.pop_add(animation, forKey: nil)
    }
    
    //按钮点击方法实现
    @objc private func didClickComposeBtn(btn: LKComposeBtn) {
        //放大被点击的按钮,缩小其他没有被点击的按钮
        UIView.animate(withDuration: 0.5, animations: {
            //遍历数组
            for composeBtn in self.btnArray
            {
                composeBtn.alpha = 0.1
                //如果数组中的btn和点击的btn相同,就进行放大操作
                if composeBtn == btn {
                    composeBtn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                }
                else{
                    composeBtn.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
                }
            }
            }) { (_) in
                //先拿到项目名
                let productName = Bundle.main.infoDictionary!["CFBundleName"] as! String
                //拼接得到类名
                let className = productName + "." + "LKComposeViewController"
                //根据类名拿到类型
                let type = NSClassFromString(className) as! UIViewController.Type
                //根据类型实例化控制器
                let vc = type.init()
                let nc = UINavigationController(rootViewController: vc)
                //跳转控制器
                self.targetVC?.present(nc, animated: true, completion: {
                    self.removeFromSuperview()
                })
        }
    }
    
    
}
